package com.qk.management.handler;


import cn.hutool.core.bean.BeanUtil;
import com.qk.code.BusinessEnum;
import com.qk.exception.BusinessException;
import com.qk.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result ex(Exception e){
        log.error("服务器发生异常:{}", e.getMessage());
        //捕获到异常之后，响应一个标准的Result
        return Result.error(BusinessEnum.System_ERROR);
    }

    @ExceptionHandler
    public Result ex(BusinessException e){
        log.error("业务异常:{}", e.getMessage());
        if (BeanUtil.isNotEmpty(e.getCode())){
            return Result.error(e.getCode());
        }
        return Result.error(BusinessEnum.BUSINESS_ERROR);
    }

}